//
//  LangViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/10.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import Localize_Swift

class LangViewController: QMUICommonViewController {

    @IBOutlet weak var tableView: UITableView!
    var langs:[LangItem] = [LangItem]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UINib(nibName: "LangTableViewCell", bundle: nil), forCellReuseIdentifier: "cell-item")
        
        print(Localize.availableLanguages())
        
        self.title = LanguageHelper.getString(key: "changeLang")
        let auto = LangItem()
        auto.name = LanguageHelper.getString(key: "SystemLang")
        self.tableView.separatorStyle = .none
        let zh = LangItem()
        zh.name = "简体中文"
        zh.locale = "zh-Hans"
        
        
        let en = LangItem()
        en.name = "English"
        en.locale = "en"
        
        let th = LangItem()
        th.name = "ภาษาไทย"
        th.locale = "th"
        langs.removeAll()
        langs.append(contentsOf: [auto,zh,en,th])
        self.tableView.reloadData()
        // Do any additional setup after loading the view.
    }
    
    
    
    func reloadLang() {
        let rootVc =  AppTabBarViewController()
        rootVc.selectedIndex = 4
        
        let changeVc = LangViewController()
        let nav =  rootVc.selectedViewController as? AppNavViewController
        changeVc.hidesBottomBarWhenPushed = true
        nav?.pushViewController(changeVc, animated: false)
        
        
        
        QMUITips.showLoading("正在切换", in: self.navigationController!.view)
        DispatchQueue.global(qos: .userInitiated).async {
            // back to the main thread
            DispatchQueue.main.async {
                UIApplication.shared.keyWindow?.rootViewController = rootVc
                QMUITips.hideAllTips()
            }
            
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension LangViewController:QMUITableViewDelegate,QMUITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return langs.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell-item") as! LangTableViewCell
        let userLang =  LanguageHelper.shareInstance.getUserLang()
        let item = langs[indexPath.row]
        cell.title.text = item.name
        if item.locale == userLang {
            cell.check.isHidden = false
        }else {
            cell.check.isHidden = true
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = langs[indexPath.row]
        LanguageHelper.shareInstance.setLanguage(langeuage: item.locale)
        
        self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        reloadLang()
    }
    
}
